﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Game.Actions;
using GamePlugin.Logics.Data;

namespace GamePlugin.Logics.Core
{
    public class SearchItemCompare
    {
        public SearchItemCompare()
        {
            this.Reset();
        }

        public void GetChangedInfo(SearchItem left, SearchItem right)
        {
            this.Reset();

            if (left == null || right == null)
                return;

            this.ItemNameChanged = left.ItemName != right.ItemName;

            if (this.ItemNameChanged)
            {
                this.Reset();
            }
            else
            {
                if (left.ItemName == null || left.ItemName.Length == 0)
                {
                    this.OccupationChanged = left.Occupation != right.Occupation;
                    this.EquipKindChanged = left.EquipKind != right.EquipKind;
                    this.ItemKindChanged = this.EquipKindChanged || (left.ItemKind != right.ItemKind);
                    this.ItemRarityChanged = left.ItemRarity != right.ItemRarity;
                }
                else
                {
                    this.OccupationChanged = false;
                    this.EquipKindChanged = false;
                    this.ItemKindChanged = false;
                    this.ItemRarityChanged = false;
                }
            }


            this.ItemBuyPriceChanged = left.ItemBuyPrice != right.ItemBuyPrice;

            this.ItemPropertyChanged[0] = this.ItemKindChanged 
                || left.Property1 != right.Property1 || left.Property1Value != right.Property1Value;

            this.ItemPropertyChanged[1] = this.ItemKindChanged
                || left.Property2 != right.Property2 || left.Property2Value != right.Property2Value;

            this.ItemPropertyChanged[2] = this.ItemKindChanged
                || left.Property3 != right.Property3 || left.Property3Value != right.Property3Value;

            this.ItemPropertyChanged[3] = this.ItemKindChanged
                || left.Property4 != right.Property4 || left.Property4Value != right.Property4Value;

            this.ItemPropertyChanged[4] = this.ItemKindChanged
                || left.Property5 != right.Property5 || left.Property5Value != right.Property5Value;

            this.ItemPropertyChanged[5] = this.ItemKindChanged
                || left.Property6 != right.Property6 || left.Property6Value != right.Property6Value;

            if (left.ItemName != right.ItemName)
                this.Reset();
        }

        private void Reset()
        {
            this.OccupationChanged = true;
            this.EquipKindChanged = true;
            this.ItemKindChanged = true;

            this.ItemNameChanged = true;
            this.ItemBuyPriceChanged = true;
            this.ItemRarityChanged = true;

            for (int i = 0; i < ActionData.PropertyCount; i++)
            {
                this.ItemPropertyChanged[i] = true;
                this.PropertyFilled[i] = true;
            }
        }

        public bool OccupationChanged = true;
        public bool EquipKindChanged = true;
        public bool ItemKindChanged = true;
        public bool ItemNameChanged = true;
        public bool ItemBuyPriceChanged = true;
        public bool ItemRarityChanged = true;
        public bool[] ItemPropertyChanged = new bool[ActionData.PropertyCount];

        public bool[] PropertyFilled = new bool[ActionData.PropertyCount];
    }
}
